import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { Icon } from '/@/components/Icon';
import { useI18n } from '/@/hooks/web/useI18n';
import { isObject } from '/@/utils/is';
const { t } = useI18n();
export const columns: BasicColumn[] = [
  {
    title: '菜单名称',
    dataIndex: 'title',
    width: 200,
    align: 'left',
    customRender: ({ record }) => {
      return t(record.title);
    },
  },
  {
    title: '多语言',
    dataIndex: 'lang',
    width: 200,
    align: 'left',
    customRender: ({ record }) => {
      return record.title;
    },
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      if (!record.icon) return '';
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '路由标识',
    dataIndex: 'name',
    width: 180,
  },
  {
    title: '权限标识',
    dataIndex: 'permission',
    width: 180,
  },
  {
    title: '组件',
    dataIndex: 'component',
    align: 'left',
  },
  {
    title: '排序',
    dataIndex: 'orderNo',
    width: 50,
    sorter: true,
  },
  {
    title: '隐藏状态',
    dataIndex: 'hideMenu',
    width: 80,
    customRender: ({ record }) => {
      const status = record.hideMenu;
      const color = status ? 'red' : 'green';
      const text = status ? '隐藏' : '显示';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      const status = record.status;
      const enable = ~~status === 0;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },
];

const isDir = (type: string) => type === '0';
const isMenu = (type: string) => type === '1';
const isButton = (type: string) => type === '2';

export const searchFormSchema: FormSchema[] = [
  {
    field: 'title',
    label: '菜单名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: '目录', value: '0' },
          { label: '菜单', value: '1' },
          { label: '按钮', value: '2' },
        ],
        onChange: (e: any) => {
          if (!e?.target?.value ?? false) return;

          if (!isDir(e.target.value)) return;
          formActionType.setFieldsValue({
            component: 'LAYOUT',
          });
        },
      };
    },
    colProps: { lg: 24, md: 24 },
  },

  {
    field: 'title',
    label: '菜单名称',
    component: 'Input',
    required: true,
    slot: 'title',
  },

  {
    field: 'parentMenu',
    label: '上级菜单',
    component: 'Input',
    slot: 'parentMenu',
  },
  {
    field: 'redirect',
    label: '重定向',
    component: 'Input',
    ifShow: ({ values }) => isDir(values.type),
  },

  {
    field: 'orderNo',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    // required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'path',
    label: '路由路径',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'name',
    label: '路由标识',
    component: 'Input',
    helpMessage: '路由Name，可用于页面跳转。且需要不能重复！',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    helpMessage: 'views文件下的文件，例如 /dashboard/analysis/index',
    show: ({ values }) => !isButton(values.type),
  },
  {
    field: 'permission',
    label: '权限标识',
    component: 'Input',
    ifShow: ({ values }) => !isDir(values.type),
  },
  {
    label: '当前活跃菜单',
    field: 'currentActiveMenu',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isMenu(values.type) && values.hideMenu,
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '禁用', value: '1' },
      ],
    },
  },
  {
    field: 'isLink',
    label: '是否外链',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },

  {
    field: 'ignoreKeepAlive',
    label: '是否缓存',
    component: 'RadioButtonGroup',
    defaultValue: true,
    componentProps: {
      options: [
        { label: '是', value: false },
        { label: '否', value: true },
      ],
    },
    ifShow: ({ values }) => isMenu(values.type),
  },

  {
    field: 'hideMenu',
    label: '隐藏菜单',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        onOptionChange: (option) => {
          formActionType.updateSchema({
            field: 'currentActiveMenu',
            required: option.hideMenu,
          });
        },
      };
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'bindLink',
    label: '绑定接口',
    component: 'Input',
    ifShow: ({ values }) => isButton(values.type),
  },
  {
    field: 'more',
    component: 'Divider',
    label: '更多设置',
    colProps: { lg: 24, md: 24, span: 24 },
    ifShow: ({ values }) => !isButton(values.type),
  },
  // bindLink
  {
    field: 'hideChildrenInMenu',
    label: '隐藏子菜单',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'hideTab',
    label: '隐藏选项卡',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'hidePathForChildren',
    label: '隐藏子项的路径',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  // affix
  {
    field: 'affix',
    label: '固定选项卡',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  // frameSrc
  {
    field: 'frameSrc',
    label: '内嵌Iframe',
    component: 'Input',
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'copyMenu',
    label: 'JSON(回车输入)',
    component: 'InputTextArea',
    labelWidth: 120,
    colProps: {
      span: 24,
      lg: 24,
      md: 24,
    },
    componentProps: ({ formActionType }) => {
      return {
        onPressEnter: (e) => {
          // console.log(JSON.parse(e.target.value));
          const res = JSON.parse(e.target.value);
          // formActionType
          // formActionType.setFieldsValue
          isObject(res) && formActionType.setFieldsValue(res);
        },
      };
    },
  },
];
